package com.phery.minicap;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DisplayJPanel extends JPanel implements RefreshListener
{
	/** 背景颜色 */
	private final Color B_COLOR = Color.LIGHT_GRAY;
	/** 起始X坐标 */
	private final int WORK_X = 10;
	/** 起始Y坐标 */
	private final int WORK_Y = 10;
	/** 当前要画的图片 */
	private BufferedImage currentImage = null;
	/** 播放延时 */
	private int delay = 50;
	/** 是否播放 */
	private boolean work = true;
	/** 是否暂停 */
	private boolean stop = false;

	/**
	 * 构造一个最初没有图片显示的DisplayJPanel对象
	 */
	public DisplayJPanel()
	{
	}

	@Override
	public void paintComponent(Graphics g)
	{
		g.setColor(B_COLOR);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		if (currentImage != null)
		{
			g.drawImage(currentImage, WORK_X, WORK_Y, null);
		}
	}

	/**
	 * 线程睡眠
	 * 
	 * @param s
	 *            睡眠时间（ms）
	 */
	private void sleep(int s)
	{
		try
		{
			Thread.sleep(s);
		} catch (Exception e)
		{
		}
	}

	/**
	 * 设置播放帧延时
	 * 
	 * @param delay
	 *            帧延时（ms）
	 */
	public void setDelay(int delay)
	{
		this.delay = delay;
	}

	/**
	 * 设置是否播放
	 * 
	 * @param work
	 *            ture 播放，false 不播放
	 */
	public void setWork(boolean work)
	{
		this.work = work;
	}

	/**
	 * 设置是否暂停
	 * 
	 * @param stop
	 *            ture 暂停，false 不暂停
	 */
	public void setStop(boolean stop)
	{
		this.stop = stop;
	}

	public static void main(String[] args)
	{
		InitDevices.init();
		JFrame frame = new JFrame("JImagePane Test");
		DisplayJPanel play = new DisplayJPanel();
		play.addMouseListener(new MouseAdapter()
		{

			int x0 = 0;
			int x1 = 0;

			public void mousePressed(MouseEvent arg0)
			{
				x0 = arg0.getX();
			}

			public void mouseReleased(MouseEvent arg0)
			{
				x1 = arg0.getX();
				excuteCMD(x1 - x0);
			}

			void excuteCMD(int x12)
			{
				int len = (int) (x12 / 2.6);
				len = Math.abs(len);
				System.out.println("手动长度：" + len);
				String cmd = "cmd.exe /k adb shell input swipe 500 500 500 500 " + len + "0";
				// System.out.println("cmd.exe /k start " + cmd);
				try
				{
					Runtime runtime = Runtime.getRuntime();
					runtime.exec(cmd);
					System.out.println("脚本执行完成！");
					Thread.sleep(1000L);
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		frame.getContentPane().add(play, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(480, 800);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		MiniCap mini = new MiniCap(play);
		mini.tryRead();
	}

	@Override
	public void refresh(byte[] buf)
	{
		byte[] temp = buf;
		new Thread()
		{
			@Override
			public void run()
			{
				try
				{
					currentImage = ImageIO.read(new ByteArrayInputStream(temp));
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (currentImage != null)
				{
					currentImage.flush();
					repaint();
				}
			}
		}.start();

	}
}